/**
 * 展示选择图片
 */
Component({
	attached() {
		const _t = this;
		if (!_t.data.upload_max_num) {
			_t.setData({
				upload_max_num: 9
			});
		}
		if (!_t.data.max_size) {
			_t.setData({
				max_size: 0
			});
		}
		_t.setData({
			is_delete_btn: _t.data.is_delete_btn
		});
	},
	methods: {
		selected() { // 选择图片
			const _t = this;
			const images = _t.data.images || [];
			wx.chooseImage({
				count: 9,
				sizeType: ['original'],
				sourceType: ['album'],
				success: (res: any) => {
					if (_t.data.upload_max_num < (images.length + res.tempFilePaths.length)) {
						throw new Error('超出最大选择数');
					} else if (_t.data.upload_max_num === (images.length + res.tempFilePaths.length)) {
						_t.setData({
							display: 'none'
						});
					}
					// 1048576
					if (_t.data.max_size !== 0) {
						res.tempFiles.forEach((item: { size: number }) => {
							if (item.size > _t.data.max_size) {
								const m = Math.round(_t.data.max_size / 1048576);
								wx.showToast({
									icon: undefined,
									title: '选择的图片不能大于' + m + 'M!'
								});
								throw new Error('选择图片错误,图片大小不能超过' + m + 'M');
							}
						});
					}
					res.tempFilePaths.forEach((item: string) => {
						images.push(item);
					});
					_t.setData({
						images
					});
				}
			});
		},
		uncheck(elem: any) { // 取消选中
			const _t = this;
			const id = elem.currentTarget.dataset.id;
			const images: string[] = [];
			_t.data.images.filter((item: string, i: number) => {
				if (i !== id) {
					images.push(item);
				}
			});
			_t.setData({
				display: 'block',
				images
			});
		},
		preview(elem: any) { // 浏览大图
			const _t = this;
			const src = elem.currentTarget.dataset.src;
			// 图片预览
			wx.previewImage({
				current: src, // 当前显示图片的http链接
				urls: _t.data.images // 需要预览的图片http链接列表
			});
		},
		clear() { // 清空
			const _t = this;
			_t.setData({
				images: [],
				unchecks: []
			});
		},
		get_image() { // 获取选择图片
			const _t = this;
			return _t.data.images;
		}
	},
	options: {},
	properties: {
		images: Object,
		is_delete_btn: Boolean,
		max_size: Number,
		upload_max_num: Number
	}
});
